import http from '../../utils/http_promise.js';
// pages/edit_house/edit_house.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
     house:{
       title:'房源标题'
     },
     fileList:[],   // 页面中显示的图片列表(包含原图)
     uploadList:[], // 上传图片列表
     uploadedImgs:[], // 上传后返回图片名称数组
     formData:{}    // 即将保存的表单数据;
  },

  onSubmit(e){

      // 1 获取当前表单数据；
      this.data.formData = e.detail.value;
      this.data.formData.id = this.data.house.id;

      // 2 上传图片
      this.upload();
      
      // 3 保存表单数据到数据库表;
     
  },

  upload() {

    // promise 对象的数组;  [promise1,promise2,promise3]
    let res = []; 

    this.data.uploadList.map((item, index) => {

      //console.log(item.path,index);

      // promise 对象;
      res[index] = new Promise((resolve, reject) => {

        // 单张图片上传;
        wx.uploadFile({

          url: 'http://127.0.0.1/jinqi_api/public/index.php/api/house/upload',
          filePath: item.path,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success: (res) => {

            console.log(res);
            
            // 把返回的图片保存到数组中;
            this.data.uploadedImgs.push(res.data);

            // 结束当前的promise
            resolve(res);

          }

        })

      })
      

    })
    

    Promise.all([...res]).then(res=>{
       
      this.data.formData.imgs = this.data.uploadedImgs;
       
      // 发起更新房源请求 
      http.request({
         method:"post",
         requireAuth:true,
         url:'house/update',
         data:this.data.formData
      })
      
      .then(res=>{
        
         if(res.data.status == "201"){
            wx.navigateTo({
              url: '/pages/my_houses/my_houses'
            })
         }

      })
      
      
    })
    

  },

  afterRead(e){
     
     // 给新选择的图片增加标识;
     e.detail.file.map(item=>{
         item.upload = true;
         return item;
     })
     
     let tmpArr = this.data.fileList;
     tmpArr = tmpArr.concat(e.detail.file);
     this.setData({
       fileList:tmpArr,
       uploadList:e.detail.file
     })
     
     
  },

  // 删除图片
  del(e){
     
     console.log(e.detail.index);
     let index = e.detail.index;
     let id = this.data.house.id;

     let house = this.data.fileList[index];
     let tmpArr = this.data.fileList;
     
     // 如果是上传图片,从显示数组删除;
     if(house.upload) {
       // 当前显示数组删除;   
       tmpArr.splice(index, 1);
       this.setData({
         fileList: tmpArr
       })
       
       return false;
       
     }
     
     // 后台api删除
     http.request({
        method:"post",
        requireAuth:true,
        url:'house/delimg',
        data:{
           id,index
        }
     })

     .then(res=>{

        tmpArr.splice(index,1);
        this.setData({
           fileList:tmpArr
        })
        
     })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

     let id = options.id;
    
     http.request({
        "method": "post",
        requireAuth: true,
        url: "house/detail",
        data: {id}
     })

     .then(res=>{
       
        console.log(res);
        let tmpArr = [];

        res.data.imgs.map(item=>{
           let obj = {};
           obj.url = item;
           tmpArr.push(obj);
        })

        this.setData({
          house: res.data,
          fileList:tmpArr
        })

     
     })
     
  },

  
})